
#include "public.h"
























static void Tim2_Init(void)
{

	TIM2_CR1=0;			//timer disenable
	TIM2_CCER2_CC3E=0;  //captuer disenable
	TIM2_CCMR3=0x01;	//input capture

	TIM2_SR1=0;			//clear status flag
	TIM2_SR2=0;
	TIM2_IER=0x08;		///time2 cc3 enable

	TIM2_PSCR=0x04;		//tim2 base =8MHz/16
	TIM2_CCER2_CC3P=1;	//detect low level/falling edge
	TIM2_CCER2_CC3E=1;	//captuer enable

	TIM2_CR1_CEN=1;		//start tim2 counter
}



void SysTimer_Init(void)
{
	Tim2_Init();

	TIM4_CR1=0x01;	//counter enable
	TIM4_IER=0x01;	//update interrupt enable
	TIM4_SR=0;		//clear status flag
	TIM4_PSCR=6;	//prescale=2^6=64
	TIM4_ARR=249;	//timer period = 1/(8MHz/64)*(249+1) =2 ms
}



 void Interrupt_Init(void)
{
	DI;


	ITC_SPR1=  (LEVEL_1<<6)  +  (LEVEL_3<<4)  +  (LEVEL_1<<2)  +  LEVEL_1  ;

	ITC_SPR2=  (LEVEL_2<<6)  +  (LEVEL_1<<4)  +  (LEVEL_1<<2)  +  LEVEL_1  ;


	ITC_SPR3=  (LEVEL_1<<6)  +  (LEVEL_3<<4)  +  (LEVEL_1<<2)  +  LEVEL_1  ;


	ITC_SPR4=  (LEVEL_3<<6)  +  (LEVEL_3<<4)  +  (LEVEL_1<<2)  +  LEVEL_1  ;


	ITC_SPR5=  (LEVEL_1<<6)  +  (LEVEL_3<<4)  +  (LEVEL_1<<2)  +  LEVEL_3  ;


	ITC_SPR6=  (LEVEL_3<<6)  +  (LEVEL_1<<4)  +  (LEVEL_3<<2)  +  LEVEL_3  ;


	ITC_SPR7=  0xff;///(LEVEL_0<<6)  +  (LEVEL_0<<4)  +  (LEVEL_0<<2)  +  LEVEL_1  ;

	ITC_SPR8= 0xff;// (LEVEL_0<<6)  +  (LEVEL_0<<4)  +  (LEVEL_0<<2)  +  LEVEL_0  ;

	EXTI_CR1 =  0b01001101;///0b01001001;
	EXTI_CR2 = 0x01;

 }

void Enable_Tim3_PWM(void)
{

	TIM3_CR1=0x0;
	TIM3_PSCR=0;	

  	TIM3_ARRH=0X1F;
	TIM3_ARRL=0XFF;
#ifdef ILLUME_IS_LUX75
    TIM3_CCR2H=0X08;
#else
 	TIM3_CCR2H=0X05;
#endif
#ifdef _DEBUG_ILLUME
 	TIM3_CCR2H=Debug_Byte;
#endif
 	TIM3_CCR2L=0XFF;


	TIM3_CCMR2=0x68;	//0x68 PWM MODE 1 ; 0X78:PWM MODE 2


	TIM3_CCER1=0x10; //active: high


	TIM3_CR1=0x1;
}


void Disable_Tim3_PWM(bool high)
{

	TIM3_CR1=0x0;
	TIM3_PSCR=0;	

  	TIM3_ARRH=0X1F;
	TIM3_ARRL=0XFF;

 	TIM3_CCR2H=0X1F;	
 	TIM3_CCR2L=0XFF;


	TIM3_CCMR2=0x68;	//0x68 PWM MODE 1 ; 0X78:PWM MODE 2


	if(high)
		TIM3_CCER1=0x10; //active: high
    else
		TIM3_CCER1=0;


	TIM3_CR1=0x1;
}
